<template>
	<view class="my-settle-container">
		<!-- 全选按钮 -->
		<view class="radio" @click="changeAllState">
			<radio color="#c00000" :checked="isCheckedAll"/><text>全选</text>
		</view>
		<!-- 总价 -->
		<view class="amount-box">
		    合计:<text class="amount">￥{{countPrice}}</text>
		</view>
		<!-- 结算按钮 -->
		<view class="btn-settle"><button @click="checkedAccount">结算({{conuntChecked}})</button></view>
	</view>
</template>

<script>
	import { mapGetters,mapMutations,mapState } from 'vuex'
	export default {
		computed: {
			...mapGetters('m_cart',['conuntChecked','total','countPrice']),
			...mapGetters('m_address',['addstr']),
			...mapState('m_address',['token']),
			...mapState('m_cart',['cart']),
			isCheckedAll(){
				return this.conuntChecked == this.total
			}
		},
		name:"my-settle",
		data() {
			return {
				// 倒计时的秒数
				seconds: 3.,
				timer: '',
				orderNumber: ''
			};
		},
		methods: {
			...mapMutations('m_address', ['updateRedirectInfo']),
			...mapMutations('m_cart',['updateAllGoodsState']),
			changeAllState(){
				this.updateAllGoodsState(!this.isCheckedAll)
			},
			checkedAccount(){
				//1. 先判断是否勾选了要结算的商品
				if(this.countPrice == null)return uni.$showMessage('请选择要结算的商品！')
				//2. 判断是否勾选了地址
				if(this.addstr == null || this.addstr == '')return uni.$showMessage('请选择收货地址！')
				//3. 最后判断用户是否登录了
				if (!this.token) return this.navigateLogin()
				this.payOrder()
			},
			//支付订单
			async payOrder(){
				const order = {
					//总价格
					order_price: 0.01,
					//地址
					consignee_addr: this.addstr,
					//订单数组
					goods: this.cart.filter(item => item.goods_state).map(i => ({goods_id: i.goods_id, goods_number: i.goods_count, goods_price: i.goods_price}))
				}
				// 1.2 发起请求创建订单
				const {data:res} = await uni.$http.post("/api/public/v1/my/orders/create",order)
				if (res.meta.status !== 200) return uni.$showMessage('创建订单失败！')
				// 1.3 得到服务器响应的“订单编号”
				this.orderNumber = res.message.order_number
				//发起预支付
				const { data: res2 } = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', { order_number: this.orderNumber })
				if (res.meta.status !== 200) return uni.$showMessage('预付订单生成失败！')
				// 2.3 得到订单支付相关的必要参数
				const payInfo = res2.message.pay
				// 3. 发起微信支付
				// 3.1 调用 uni.requestPayment() 发起微信支付
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: payInfo.timeStamp,
					nonceStr: payInfo.nonceStr,
					package: payInfo.package,
					signType: payInfo.signType,
					paySign: payInfo.paySign,
					success: function (res) {
						console.log('success:' + JSON.stringify(res));
					},
					fail: function (err) {
						console.log('fail:' + JSON.stringify(err));
					}
				})
				// console.log(payInfo)
				// 3.2 未完成支付
			    // if (err) return uni.$showMessage('订单未支付！')
				console.log(res2)
			},
			//倒计时登录跳转
			navigateLogin(){
				//当第二次第三次点击的时候secodes值没重置
				this.seconds = 3
				 // 1. 展示提示消息，此时 seconds 的值等于 3
				this.showTips(this.seconds)
				  // 2. 创建定时器，每隔 1 秒执行一次
				this.timer = setInterval(() =>{
					// 2.1 先让秒数自减 1
					this.seconds--
					if(this.seconds <= 0){
						//清除定时器
						clearInterval(this.timer)
						uni.switchTab({
							url:'/pages/my/my',
							success: () => {
								// 调用 vuex 的 updateRedirectInfo 方法，把跳转信息存储到 Store 中
								this.updateRedirectInfo({
									//跳转的方式
									openType: 'switchTab',
									//从哪个页面跳转过去的
									info: '/pages/cart/cart'
								})
							}
						})
						return
					}
					// 2.2 再根据最新的秒数，进行消息提示
					this.showTips(this.seconds)
				},1000)
			},
			//提示方法
			showTips(s) {
				uni.showToast({
					// 不展示任何图标
					icon: 'none',
					// 提示的消息
					title: '请登录后再结算！' + s + ' 秒后自动跳转到登录页',
					// 为页面添加透明遮罩，防止点击穿透
					mask: true,
					// 1.5 秒后自动消失
					duration: 1500
				})
			}
		}
	}
</script>

<style lang="scss">
.my-settle-container{
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
	height: 50px;
	background-color: white;
	z-index: 99;
	display: flex;
	justify-content: space-between;
	padding-left: 5px;
	align-items: center;
	font-size: 14px;
	.radio{
		display: flex;
		align-items: center;
	}
	.amount-box{
		color: #c00000;
	}
	.btn-settle{
		height: 50px;
		line-height: 50px;
		button{
			height: 100%;
			width: 100px;
			color: white;
			background-color: #c00000;
			align-items: center;
			padding: 0 10px;
		}
	}
}
</style>